package com.tiger.lbs;

import java.util.ArrayList;
import java.util.List;

public class DrivingRoutes {
	private List<DrivingRoute> routes;
	private List<Coordinate> path;
	
	public List<DrivingRoute> getRoutes() {
		return routes;
	}

	public void setRoutes(List<DrivingRoute> routes) {
		this.routes = routes;
	}

	public void merge(DrivingRoutes tmp) {
		this.routes.addAll(tmp.routes);
	}
	
	public List<Coordinate> getPath() {
		if(this.path == null) {
			List<Coordinate> path = new ArrayList<>();
			for(int i = 0; routes != null && i < routes.size(); i++) {
				DrivingRoute route = routes.get(i);
				path.addAll(route.getPath());
			}
			this.path = path;
		}
		return this.path;
	}

	public int getClosestIndex(Coordinate pos) {
		double min = Double.MAX_VALUE;
		int index = 0;
		List<Coordinate> path = getPath();
		for(int i = 0; i < path.size(); i++) {
			Coordinate coord = path.get(i);
			double cur = coord.distance(pos);
			if(cur < min) {
				min = cur;
				index = i;
			}
		}
		return index;
	}
	public int getFirstClosestIndex(Coordinate pos, int radius) {
		List<Coordinate> path = getPath();
		for(int i = 0; i < path.size(); i++) {
			Coordinate coord = path.get(i);
			if(coord.isInRangeOf(pos, radius)) {
				return i;
			}
		}
		return -1;
	}
	
	public int getDistance() {
		int distance = 0;
		if(routes != null) {
			for(DrivingRoute route : routes) {
				distance += route.getDistance();
			}
		}
		return distance;
	}
	
	public int getDuration() {
		int duration = 0;
		if(routes != null) {
			for(DrivingRoute route : routes) {
				duration += route.getDuration();
			}
		}
		return duration;
	}

	public Coordinate get(int index) {
		return getPath().get(index);
	}

//	public void remainOne() {
//		if(routes.size() > 1) {
//			System.out.println("===============");
//			DrivingRoute r = routes.get(0);
//			routes = new ArrayList<>();
//			routes.add(r);
//		}
//	}
}
